package com.jinke.uang.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jinke.uang.R;
import com.jinke.uang.constant.Constant;
import com.jinke.uang.bean.OrderItemData;
import com.jinke.uang.utils.StringUtils;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 已退出订单的
 */
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.MyViewHolder> {

    private List<OrderItemData> datas;
    private Context context;

    public OrderAdapter(Context context, List<OrderItemData> datas) {
        this.context = context;
        this.datas = datas;

    }


    public List<OrderItemData> getDatas() {
        return datas;
    }

    public void setDatas(List<OrderItemData> datas) {
        this.datas = datas;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_borrow_order,parent,false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (holder == null) {
            return;
        }
        OrderItemData onetime = datas.get(position);
        holder.tvId.setText(String.format("%s %s", context.getResources().getString(R.string
                .borrow_detail_page_order_code), onetime.getOrderId()));
        // 审批状态: 1审核中 2 待放款  3 已放款,待还款  4 已拒绝 5 已结清 6 已逾期 7 已展期

        String status = "";
        switch (onetime.getStatus()) {
            case Constant.ORDER_STATUS_AUDITING:
                //1审核中
                status = context.getResources().getString(R.string.repayment_type_auditing);
                break;
            case Constant.ORDER_STATUS_LENDING:
                //3 已放款,待还款
                status = context.getResources().getString(R.string.repayment_type_lending);
                break;
            case Constant.ORDER_STATUS_REJECT:
                //5 已拒绝
                status = context.getResources().getString(R.string.repayment_type_reject);
                holder.tvTime.setVisibility(View.GONE);
                break;
            case Constant.ORDER_STATUS_CLOSEOUT:
                //5 已结清
                status = context.getResources().getString(R.string.repayment_type_closeout);
                holder.tvTime.setVisibility(View.GONE);
                break;
            case Constant.ORDER_STATUS_OVERDUE:
                //6 已逾期
                status = context.getResources().getString(R.string.repayment_type_overdue);
                break;
            case Constant.ORDER_STATUS_ROLLOVER:
                //7 已展期
                status = context.getResources().getString(R.string.repayment_type_rollover);
                break;
            default:
                break;
        }
        holder.tvStatus.setText(status);
        String moneyUnit = context.getResources().getString(R.string.money_unit);
        holder.tvMoney.setText(String.format("%s: %s%s", context.getResources().getString(R
                .string.borrow_detail_page_loan_of_num), moneyUnit, StringUtils.getDotMoneyStr
                (String.valueOf(onetime.getApprovedAmt()))));
        holder.tvTime.setText(String.format("%s: %s", context.getResources().getString(R.string
                .borrow_detail_page_expiration_time), onetime.getLastPayDate()));
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }


    static class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.ivImg)
        ImageView ivImg;
        @BindView(R.id.tvId)
        TextView tvId;
        @BindView(R.id.tvStatus)
        TextView tvStatus;
        @BindView(R.id.tvMoney)
        TextView tvMoney;
        @BindView(R.id.tvTime)
        TextView tvTime;

        MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}

